package org.zachary.agent.interfaces.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.zachary.agent.application.exception.ApplicationException;
import org.zachary.agent.domain.exception.DomainException;
import org.zachary.agent.infrastructure.utils.Result;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(DomainException.class)
    public Result<String> handleDomainException(DomainException e) {
        log.error(e.getMessage(), e);
        return Result.exception(e.getMessage());
    }

    @ExceptionHandler(ApplicationException.class)
    public Result<String> handleApplicationException(ApplicationException e) {
        log.error(e.getMessage(), e);
        return Result.exception(e.getMessage());
    }

    @ExceptionHandler(ApplicationContextException.class)
    public Result<String> handleContextException(ApplicationContextException e) {
        log.error(e.getMessage(), e);
        return Result.exception("系统配置错误，请联系管理员");
    }

    @ExceptionHandler(Exception.class)
    public Result<String> handleException(Exception e) {
        log.error(e.getMessage(), e);
        return Result.exception(e.getMessage());
    }
}
